package cn.csust.net2.manager.server.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;

import cn.csust.net2.manager.server.dao.BaseDAO;
import cn.csust.net2.manager.server.dao.ParameterDAO;
import cn.csust.net2.manager.server.remote.BeanUtils;
import cn.csust.net2.manager.server.util.Util;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Parameter;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.StudentInformationService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;
@Service(value = ServiceConstant.SERVICE_NAME_STUDENTINFO)
public class StudentInformationServiceImpl  extends BaseServiceImpl  implements  StudentInformationService{

	@Resource(name = Constant.DAO_NAME_PARAMETER)
	private ParameterDAO parameterDAO;
	/**
	 * 查找全部
	 */
	@Transactional
	@Override
	public <T extends PO>  BasePagingLoadResult<T> findAll(String cla,PagingLoadConfig config)throws BeyondAuthorityException {
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		Student student = config.get("student");
		String nianji = config.get(Constant.CONFIG_GRADE);
		StringBuffer s = new StringBuffer(" from "+cla+" c where 1=1 ");
		
		if(student!=null){
			System.out.println("加载表格的用户名为"+student.getName());
			s.append(" and c.user.userID = "+student.getUserID());
		}
		if(nianji!=null&&!nianji.equals("")){
			
			s.append(" and c.schoolYear = '"+nianji+"'");
			
		}else{
			
			s.append(" and c.schoolYear = '"+Util.getNowTerm(student.getEntryDate())+"'");
		}
		return dao.findAllByPaging(config,s.toString());
	}
	private boolean test(String paraName) throws ParseException{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Parameter infoRegister_1 = parameterDAO.findUniqueHQL_T("from Parameter where paraName = '"+paraName+"'");
		Parameter infoRegister_1_last = parameterDAO.findUniqueHQL_T("from Parameter where  paraName = '"+paraName+"_last'");
		int last = Integer.valueOf(infoRegister_1_last.getValue());
	
			Date s = df.parse(infoRegister_1.getValue());
			Date date = Util.judgeYearNow(s);
			if(date!=null){
				infoRegister_1.setValue(df.format(date));
				parameterDAO.save(infoRegister_1);
				s = df.parse(infoRegister_1.getValue());
			}
			return Util.juge(s, last);
	}
	@Transactional
	@Override
	public boolean testEdit() {
		
		try {
			return this.test("infoRegister_1") ||this.test("infoRegister_2");
			
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return false;
	}

	public ParameterDAO getParameterDAO() {
		return parameterDAO;
	}

	public void setParameterDAO(ParameterDAO parameterDAO) {
		this.parameterDAO = parameterDAO;
	}
}
